#include <ptgui/ptgui.h>

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

int ptgui_strncasecmp(const rt_int8_t *s1, const rt_int8_t *s2, size_t len)
{
	unsigned int x2;
	unsigned int x1;
	const rt_int8_t *end = s1 + len;

	while (1) {
		if ((s1 >= end)) {
			return 0;
		}

		x2 = *s2 - 'A'; if ((x2 < 26u)) {
							x2 += 32;
						}
		x1 = *s1 - 'A'; if ((x1 < 26u)) {
							x1 += 32;
						}
		s1++; s2++;

		if ((x2 != x1)) {
			break;
		}

		if ((x1 == (unsigned int) - 'A')) {
			break;
		}
	}

	return x1 - x2;
}

void * ptgui_memset(void *src, int c, rt_size_t n)
{
	return memset(src, c, n);
}

void * ptgui_memcpy(void *dst, const void *src, rt_size_t n)
{
	return memcpy(dst, src, n);
}

int ptgui_memcmp(const void *dst, const void *src, rt_size_t n)
{
	return memcmp(dst, src, n);
}

void * ptgui_memmove(void *dst, const void *src, rt_size_t count)
{
	return memmove(dst, src, count);
}

rt_size_t ptgui_strncmp(const rt_int8_t *cs, const rt_int8_t *ct, rt_size_t count)
{
	return strncmp(cs, ct, count);
}

rt_size_t ptgui_strlen(const rt_int8_t *src)
{
	return strlen(src);
}

rt_int8_t * ptgui_strncpy(rt_int8_t *dest, const rt_int8_t *src, rt_size_t n)
{
	rt_int8_t *dscan;
	const rt_int8_t *sscan;

	dscan = dest;
	sscan = src;
	while (n > 0) {
		--n;
		if ((*dscan++ = *sscan++) == '\0') {
			break;
		}
	}

	while (n-- > 0)
		*dscan++ = '\0';

	return dest;
}

rt_int8_t * ptgui_strdup(const rt_int8_t *str)
{
	rt_int8_t *tmp = (rt_int8_t *)ptgui_malloc(strlen(str) + 1);
	if (!tmp) {
		return 0;
	}
	strcpy(tmp, str);

	return tmp;
}

void ptgui_snprintf(rt_int8_t *buffer, rt_size_t count, const rt_int8_t *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	_vsnprintf(buffer, count, fmt, args);
	va_end(args);
}

void ptgui_sprintf(rt_int8_t *buffer, const rt_int8_t *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vsprintf(buffer, fmt, args);
	va_end(args);
}